`
https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var findUnsortedSubarray = function (nums) {
  const n = nums.length
  let maxn = -Infinity, minn = Infinity
  let left = -1, right = -1

  for (let i = 0; i < n; i++) {
    // 如果进入了右段，就没有比最大值小的数，所以最后一个比最大值小的数就是中段的右边界
    if (maxn > nums[i]) {
      right = i
    } else {
      maxn = nums[i]
    }
    // 同理
    if (minn < nums[n - i - 1]) {
      left = n - i - 1
    } else {
      minn = nums[n - i - 1]
    }
  }

  return right === -1 ? 0 : right - left + 1
};